package com.wj.rspmproperty.mapper;

import com.github.pagehelper.Page;
import com.wj.rspmproperty.pojo.Pay;
import com.wj.rspmproperty.pojo.vo.PayInfo;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;

/**
 * @author WeiJin
 * @version 1.0
 */
public interface PayMapper {

    /**
     * 带条件的分页查询支付信息
     *
     * @param start
     * @param end
     * @param username
     * @param idCard
     * @param floorId
     * @param buildingId
     * @return
     */
    @Select("<script>"+"select payInfo.pay_id,payInfo.building_id,payInfo.room_id,u.userid " +
            "from " +
            "(select p.pay_id,b.building_id,r.room_id,f.floor_id from " +
            "(select pay.pay_id,pay.pay_room_id from pay " +
            "<where> " +
            "<if test='start != null and end != null'> " +
            "pay.pay_date BETWEEN #{start} and #{end} " +
            "</if> " +
            "</where> ) " +
            "p,building b,(select r.room_id,r.room_floor_id from room r "+
            "<where> <if test ='floorId != null'> r.room_floor_id = #{floorId} </if> </where> " +
            "union select room.room_id,room.room_floor_id from room,floor " +
            "WHERE " +
            "<if test='buildingId != null and floorId == null'> " +
            "floor.belong_building = #{buildingId} and </if> " +
            "room.room_floor_id = floor.floor_id ) r,floor f " +
            "where r.room_floor_id = f.floor_id and f.belong_building = b.building_id and p.pay_room_id = r.room_id ) payInfo " +
            "LEFT JOIN " +
            "(select userid,user_roomid from `user` u " +
            "<where> " +
            "<if test = 'username != null and username != \"\"'> u.username like concat('%',#{username},'%') </if>" +
            "<if test = 'idCard != null and idCard != \"\"'> and u.idcard like concat('%',#{idCard},'%') </if>" +
            "</where> ) u on u.user_roomid = payInfo.floor_id " +
            "</script>")
    @Results(id = "payInfo", value = {
            @Result(property = "user", column = "userid", one = @One(
                    select = "com.wj.rspmproperty.mapper.UserMapper.queryById")),
            @Result(property = "building", column = "building_id", one = @One(
                    select = "com.wj.rspmproperty.mapper.BuildingMapper.queryById")),
            @Result(property = "room", column = "room_id", one = @One(
                    select = "com.wj.rspmproperty.mapper.RoomMapper.getRoomById")),
            @Result(property = "pay", column = "pay_id", one = @One(
                    select = "com.wj.rspmproperty.mapper.PayMapper.queryById"))
    })
    Page<PayInfo> selectPayInfoOnCondition(@Param("start") LocalDate start, @Param("end") LocalDate end,
                                           @Param("username") String username,@Param("idCard") String idCard,
                                           @Param("floorId") Integer floorId, @Param("buildingId") Integer buildingId);


    /**
     * 根据id查询支付信息
     * @param id
     * @return
     */
    @Select("<script>" +
            "select * from pay where pay_id = #{id}" +
            "</script>")
    Pay queryById(Integer id);

}
